package com.melody.android.ui.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import com.melody.android.databinding.FragmentMineBinding
import com.melody.android.logic.model.SongList
import com.melody.android.logic.util.setPrimaryDesign
import com.melody.android.logic.util.setSongLists
import com.melody.android.ui.component.BottomSheetCreateSongList
import com.melody.android.ui.viewModel.SongViewModel

class MineFragment : Fragment() {

    private lateinit var binding: FragmentMineBinding
    private lateinit var songViewModel: SongViewModel
    private val bottomSheet = BottomSheetCreateSongList()

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
    ): View {
        binding = FragmentMineBinding.inflate(inflater, container, false)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        // 获取MainActivity所持有的SongViewModel
        songViewModel = ViewModelProvider(requireActivity())[SongViewModel::class.java]
        initView()
    }

    private fun initView() {
        binding.imgUserImage.setPrimaryDesign()
        songViewModel.songLists.observe(requireActivity()) { songLists ->
            binding.rvSongList.setSongLists(songViewModel, songLists as ArrayList<SongList>)
        }
        binding.imgAdd.setOnClickListener {
            bottomSheet.show(requireActivity().supportFragmentManager, BottomSheetCreateSongList.TAG)
        }
    }

}